package com.taiyuan.tongcheng.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taiyuan.tongcheng.widget.dialog.anim.BaseEffects;
import com.tytc56.user.R;

/**
 * 
* @Description 设置项弹出框
* @author wzq
* @date 2015-7-4 上午9:20:55
* @update (date)
* @version V1.0
 */
public class TcBuilder extends Dialog implements DialogInterface ,android.view.View.OnClickListener {
	
	
//  头标题控件、
	private TextView  settingTitleTv;
	private TextView  settingContentTv;
	private Button    settingOkBtn;
	private Button    settingCancelBtn;
//  内容view
	private View  mDialogView;
//  内部布局
	private LinearLayout settingLayout;
	
    private Effectstype type=null;
    

    private int mDuration = -1;
    private static  int mOrientation=1;
    private boolean isCancelable=true;
    private volatile static TcBuilder instance;
    private Context context;
    

    public TcBuilder(Context context) {
        super(context);
        init(context);
    }
    
    public TcBuilder(Context context,int theme) {
        super(context, theme);
        init(context);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        params.width  = ViewGroup.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

    }

    public static TcBuilder getInstance(Context context){

        if (instance == null) {
            synchronized (TcBuilder.class) {
                if (instance == null) {
                    instance = new TcBuilder(context,R.style.dialog_untran);
                }
            }
        }
        return instance;

    }
    public static TcBuilder getInstance(Context context,int i){
    	
    	return new TcBuilder(context,R.style.dialog_untran);
    	
    }

	private void init(Context context) {

		mDialogView = View.inflate(context, R.layout.layout_common_dialog, null);

	    settingTitleTv=(TextView)mDialogView.findViewById(R.id.tv_inform_title);
	    settingContentTv=(TextView)mDialogView.findViewById(R.id.tv_inform_content);
		settingOkBtn=(Button)mDialogView.findViewById(R.id.btn_inform_ok);
		settingCancelBtn=(Button)mDialogView.findViewById(R.id.btn_inform_cancel);
		settingCancelBtn.setVisibility(View.GONE);
		settingLayout=(LinearLayout)mDialogView.findViewById(R.id.inform_ll);
		setContentView(mDialogView);

		this.setOnShowListener(new OnShowListener() {
			@Override
			public void onShow(DialogInterface dialogInterface) {

				settingLayout.setVisibility(View.VISIBLE);
				if (type == null) {
					type = Effectstype.Slidetop;
				}
				start(type);

			}
		});
		settingLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (isCancelable)
					dismiss();
			}
		});
		getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
	}




    public TcBuilder withTitle(CharSequence title) {
        settingTitleTv.setText(title);
        return this;
    }

    public TcBuilder withTitleColor(String colorString) {
    	settingTitleTv.setTextColor(Color.parseColor(colorString));
        return this;
    }

    public TcBuilder withContent(int textResId) {
    	settingContentTv.setText(textResId);
        return this;
    }

    public TcBuilder withContent(CharSequence msg) {
    	settingContentTv.setText(msg);
        return this;
    }

    public TcBuilder withDuration(int duration) {
        this.mDuration=duration;
        return this;
    }

    public TcBuilder withEffect(Effectstype type) {
        this.type=type;
        return this;
    }
    
    public TcBuilder withButton1Text(CharSequence text) {
        settingOkBtn.setVisibility(View.VISIBLE);
        settingOkBtn.setText(text);
        return this;
    }
    public TcBuilder withButton2Text(CharSequence text) {
        settingCancelBtn.setVisibility(View.VISIBLE);
        settingCancelBtn.setText(text);
        return this;
    }
    
    
    public TcBuilder setButton1Click(View.OnClickListener click ) {
    	settingOkBtn.setOnClickListener(click);
        return this;
    }
    

    public TcBuilder setButton2Click(View.OnClickListener click ) {
    	settingCancelBtn.setVisibility(View.VISIBLE);
    	settingCancelBtn.setOnClickListener(click);
        return this;
    }

    public TcBuilder isCancelableOnTouchOutside(boolean cancelable) {
        this.isCancelable=cancelable;
        this.setCanceledOnTouchOutside(cancelable);
        return this;
    }

    public TcBuilder isCancelable(boolean cancelable) {
        this.isCancelable=cancelable;
        this.setCancelable(cancelable);
        return this;
    }

    @Override
    public void show() {
        super.show();
    }

    private void start(Effectstype type){
        BaseEffects animator = type.getAnimator();
        if(mDuration != -1){
            animator.setDuration(Math.abs(mDuration));
        }
        animator.start(settingLayout);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
    
	@Override
	public void onClick(View v) {}
    
    
    
}
